package com.joolun.web.controller.paynow;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joolun.common.annotation.Anonymous;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.system.domain.*;
import com.joolun.system.service.*;
import com.joolun.web.ipaynow.PayUtil;
import com.joolun.web.ipaynow.UUOrder;
import com.joolun.web.ipaynow.utils.MD5Facade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付Controller
 *
 * @author return
 * @date 2025-07-04
 */
@RestController
@RequestMapping("/pay/now")
public class PayController extends BaseController {

    @Autowired
    private IPayParamService payParamService;

    @Autowired
    private IPayLinkService iPayLinkService;

    @Autowired
    private IPayOrderService iPayOrderService;

    @Autowired
    private IEduCourseService eduCourseService;
    /**
     *
     * 获取支付落地页参数配置
     * @param
     * @return
     */
    @GetMapping("/param/{code}")
    @Anonymous
    public AjaxResult payParam(@PathVariable("code") String code){
        // 获取支付链接落地页配置
        PayLink payLink = iPayLinkService.getOne(Wrappers.lambdaQuery(PayLink.class).eq(PayLink::getCode,code));
        // 获取课程信息
        EduCourse eduCourse = eduCourseService.selectEduCourseByCourseId(Long.parseLong(payLink.getCourseId()));
        AjaxResult ajax = AjaxResult.success();
        ajax.put("payLink", payLink);
        ajax.put("eduCourse", eduCourse);
        return ajax;
    }

    /**
     *
     * 获取支付下单参数,同时生成订单
     * @param
     * @return
     */
    @PostMapping("/order")
    @Anonymous
    public AjaxResult payOrder(@RequestBody Map<String, Object> params){
        // 支付参数id
        Integer paramId = (Integer) params.get("paramId");
        // 支付链接id
        Integer lindId = (Integer) params.get("lindId");
        // 用户UnionId
        String unionId = (String) params.get("unionId");
        PayLink payLink = iPayLinkService.selectPayLinkById(Long.parseLong(lindId.toString()));
        // 获取支付参数
        PayParam payParam = payParamService.selectPayParamById(Long.parseLong(paramId.toString()));
        // 生成订单号
        String orderNo = UUOrder.generateOrderNo();
        System.out.println("-----------订单号：" + orderNo);
        // 封装订单基础参数
        PayOrder payOrder = new PayOrder();
        payOrder.setOrderNo(orderNo);
        // 用户唯一标识
        payOrder.setUnionId(unionId);
        // 创建订单
        PayOrder newPayOrder = iPayOrderService.insertPayOrder(payOrder, payLink);
        if(newPayOrder != null){
            // 补全支付配置参数
            // 订单号
            payParam.setMhtOrderNo(orderNo);
            // 订单名称
            payParam.setMhtOrderName(newPayOrder.getName());
            // 订单金额
            payParam.setMhtOrderAmt(newPayOrder.getPaymentPrice());
            // 订单描述
            payParam.setMhtOrderDetail(newPayOrder.getOrderDetail());
            // 获取订单时间并格式化
            String formattedDate = PayUtil.formatDateToStr(newPayOrder.getCreateTime());
            // 订单开始时间
            payParam.setMhtOrderStartTime(formattedDate);
            // 获取签名
            Map<String, String> dataMap = PayUtil.getMhtSignature(payParam);
            return success(dataMap);
        }else{
            return error("订单创建失败，请联系客服！");
        }
    }

}
